package com.lyf.shop.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.lyf.common.core.domain.R;
import com.lyf.common.core.utils.StringUtils;
import com.lyf.common.core.utils.poi.ExcelUtil;
import com.lyf.common.core.web.controller.BaseController;
import com.lyf.common.core.web.domain.AjaxResult;
import com.lyf.common.core.web.page.TableDataInfo;
import com.lyf.common.log.annotation.Log;
import com.lyf.common.log.enums.BusinessType;
import com.lyf.common.security.annotation.InnerAuth;
import com.lyf.common.security.annotation.RequiresPermissions;
import com.lyf.system.api.domain.Supermarket;
import com.lyf.shop.service.ISupermarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 超市Controller
 * 
 * @author lyf
 * @date 2023-02-09
 */
@RestController
@RequestMapping("/supermarket")
public class SupermarketController extends BaseController
{
    @Autowired
    private ISupermarketService supermarketService;

    /**
     * 查询超市列表
     */
    @RequiresPermissions("supermarket:supermarket:list")
    @GetMapping("/list")
    public TableDataInfo list(Supermarket supermarket)
    {

        startPage();
        List<Supermarket> list = supermarketService.selectSupermarketList(supermarket);
        return getDataTable(list);
    }

    /**
     * 查询超市列表
     */
    @InnerAuth
    @PostMapping("/listAndUser")
    public R<List<Supermarket>> listAndUser(@RequestBody Supermarket supermarket)
    {
        List<Supermarket> list = supermarketService.selectSupermarketList(supermarket);
        return R.ok(list);
    }
    /**
     * 导出超市列表
     */
    @RequiresPermissions("supermarket:supermarket:export")
    @Log(title = "超市", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supermarket supermarket)
    {
        List<Supermarket> list = supermarketService.selectSupermarketList(supermarket);
        ExcelUtil<Supermarket> util = new ExcelUtil<Supermarket>(Supermarket.class);
        util.exportExcel(response, list, "超市数据");
    }

    /**
     * 获取超市详细信息
     */
    @RequiresPermissions("supermarket:supermarket:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supermarketService.selectSupermarketById(id));
    }


     @RequiresPermissions("supermarket:supermarket:query")
     @GetMapping(value = "/user/{userId}")
    public R<List<Long>> selectSupermarketListByUserId(@PathVariable("userId") Long userId){
         if (!StringUtils.isNull(userId)){
             return R.ok(supermarketService.selectSupermarketListByUserId(userId));
         }

         return R.fail("用户id不能为空");
    }

    @RequiresPermissions("supermarket:supermarket:query")
    @GetMapping(value = "/supermarketName/{userId}")
    public AjaxResult selectSupermarketNameByUserId(@PathVariable("userId") Long userId){
           if (StringUtils.isNotNull(userId)) {
               List<Supermarket> supermarkets = supermarketService.selectSupermarketNameByUserId(userId);
               return AjaxResult.success(supermarkets);
           }
           return AjaxResult.error("用户id不能为空");
    }
    /**
     * 新增超市
     */
    @RequiresPermissions("supermarket:supermarket:add")
    @Log(title = "超市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supermarket supermarket)
    {
        return toAjax(supermarketService.insertSupermarket(supermarket));
    }

    /**
     * 修改超市
     */
    @RequiresPermissions("supermarket:supermarket:edit")
    @Log(title = "超市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supermarket supermarket)
    {
        return toAjax(supermarketService.updateSupermarket(supermarket));
    }

    /**
     * 删除超市
     */
    @RequiresPermissions("supermarket:supermarket:remove")
    @Log(title = "超市", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supermarketService.deleteSupermarketByIds(ids));
    }
}
